package org.healthy.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.healthy.controller.base.BaseController;
import org.healthy.model.user.User;
import org.healthy.model.page.Pagination;
import org.healthy.service.IUserService;
import org.healthy.utils.pub.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * 用户
 */
@Controller
@RequestMapping("/api")
public class UserController extends BaseController {

    @Resource(name = "userService")
    private IUserService userService;

    /**
     * 用户注册,并返回用户信息
     *
     * @param user 用户对象
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/register", method = RequestMethod.POST)
    public String registerUser(@RequestBody User user) {
        User v = userService.registerUser(user);
        return response(v);
    }

    /**
     * 发送手机验证码
     * @param mobile
     * @return
     */
     @ResponseBody
     @RequestMapping
     public  String sendMobileCode(@RequestParam String mobile)
     {
         userService.sendMobileCode(mobile);
         return response(null);
     }
    /**
     * 用户登录
     *
     * @param mobile   手机号
     * @param password 密码
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/login", method = RequestMethod.GET)
    public String login(@RequestParam String mobile, @RequestParam String password) {
        User user = userService.userLogin(mobile, password);
        return response(user);
    }


    /**
     * QQ,微信,新浪登录
     * @param openid
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/thridlogin", method = RequestMethod.GET)
    public String loginthrid(@RequestParam String openid, @RequestParam String type) {
        User user = userService.userLogin(openid, type);
        return response(user);
    }




    /**
     * 用户手机验证码登录
     * 根据用户手机验证码登录
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/logincode", method = RequestMethod.GET)
    public String loginBycode(@RequestParam String mobile, @RequestParam String code) {
        User user = userService.userLoginByCode(mobile, code);
        return response(user);
    }
    /**
     * 用户密码重置
     *
     * @param password
     * @param mobile
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/resetPwd", method = RequestMethod.POST)
    public String resetPwd(@RequestParam String password, @RequestParam String mobile) {
        userService.resetPassWord(mobile, password);
        return response(null);
    }
    /**
     * 用户基本资料修改
     *
     * @param user
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/modify", method = RequestMethod.POST)
    public String modifyUser(@RequestBody User user) {
        if (user == null) return "";
        user.setUid(getHeader().getUid());
        userService.update(user);
        return response(null);
    }

    /**
     * 判断手机号码是否存在
     * @param mobile
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/v1/user/isexist/{mobile}", method = RequestMethod.GET)
    public String userIsExists(@PathVariable String mobile) {
        Boolean isExist = userService.isUserExist(mobile);
        return response(isExist);

    }



}